package com.zpark.it.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.x.protobuf.Mysqlx;
import com.zpark.it.pojo.Admin;
import com.zpark.it.service.AdminService;
import com.zpark.it.utils.MD5;
import com.zpark.it.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: AdminController
 * @Author Charles
 * @Package com.zpark.it.controller
 * @Date 2023/7/29 21:17
 */
@Api(tags = "系统管理控制器")
@RestController
@RequestMapping("/sms/adminController")
public class AdminController {


    @Autowired
    private AdminService adminService;

    /**
     * 带条件的分页查询管理员信息
     * @param pageNo
     * @param pageSize
     * @param adminName
     * @return
     */
    @ApiOperation("带条件的分页查询管理员信息")
    @GetMapping("getAllAdmin/{pageNo}/{pageSize}")
    public Result getAllAdmin( @ApiParam("页码数")@PathVariable Integer pageNo,
                              @ApiParam("页码大小")@PathVariable Integer pageSize,
                              @ApiParam("管理员名字") String adminName){

        Page<Admin> pageParam=new Page<>(pageNo,pageSize);
        IPage<Admin> iPage=adminService.getAdminsByOpr(pageParam,adminName);
        return Result.ok(iPage);

    }

    @ApiOperation("增加或者修改管理员信息")
    @PostMapping("saveOrUpdateAdmin")
    public Result saveOrUpdateAdmin(@ApiParam("管理员json格式的对象")@RequestBody Admin admin){
        Integer id = admin.getId();
        if (id==null||id==0) {
            admin.setPassword(MD5.encrypt(admin.getPassword()));
        }
        adminService.saveOrUpdate(admin);
        return Result.ok();
    }


    @ApiOperation("删除管理员信息")
    @DeleteMapping("deleteAdmin")
    public Result deleteAdmin( @ApiParam("删除的id")@RequestBody List<Integer> ids){
        adminService.removeByIds(ids);
        return Result.ok();
    }
}
